package com.blq.web.ServeTemplate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.blq.common.utils.StringUtils;
import com.blq.common.core.page.TableDataInfo;
import com.blq.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.blq.web.ServeTemplate.domain.bo.ServeTemplateBo;
import com.blq.web.ServeTemplate.domain.vo.ServeTemplateVo;
import com.blq.web.ServeTemplate.domain.ServeTemplate;
import com.blq.web.ServeTemplate.mapper.ServeTemplateMapper;
import com.blq.web.ServeTemplate.service.IServeTemplateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 业务模板Service业务层处理
 *
 * @author Blq
 * @date 2022-11-07
 */
@RequiredArgsConstructor
@Service
public class ServeTemplateServiceImpl extends ServiceImpl<ServeTemplateMapper,ServeTemplate> implements IServeTemplateService {

    private final ServeTemplateMapper baseMapper;

    /**
     * 查询业务模板
     */
    @Override
    public ServeTemplateVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询业务模板列表
     */
    @Override
    public TableDataInfo<ServeTemplateVo> queryPageList(ServeTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ServeTemplate> lqw = buildQueryWrapper(bo);
        Page<ServeTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业务模板列表
     */
    @Override
    public List<ServeTemplateVo> queryList(ServeTemplateBo bo) {
        LambdaQueryWrapper<ServeTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ServeTemplate> buildQueryWrapper(ServeTemplateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ServeTemplate> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ServeTemplate::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getClassify()), ServeTemplate::getClassify, bo.getClassify());
        lqw.eq(StringUtils.isNotBlank(bo.getContext()), ServeTemplate::getContext, bo.getContext());
        return lqw;
    }

    /**
     * 新增业务模板
     */
    @Override
    public Boolean insertByBo(ServeTemplateBo bo) {
        ServeTemplate add = BeanUtil.toBean(bo, ServeTemplate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改业务模板
     */
    @Override
    public Boolean updateByBo(ServeTemplateBo bo) {
        ServeTemplate update = BeanUtil.toBean(bo, ServeTemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ServeTemplate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除业务模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
